//
//  CAHomeCollectionCell.m
//  cascade-realm
//
//  Created by QSC on 22/5/24.
//

#import "CAHomeCollectionCell.h"
@interface CAHomeCollectionCell()
@property (nonatomic,strong) UIImageView *bgImgv;
@property (nonatomic,strong) UIButton * iconBtn;
@property (nonatomic,strong) UIImageView *addImgV;
@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UIButton * likeBtn;
@property (nonatomic,strong) UILabel * titleLabel;

@end
@implementation CAHomeCollectionCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initView];
        self.clipsToBounds = true;
        self.layer.cornerRadius = 4;
    }
    return  self;
}

- (void)setModel:(CADataModel *)model{
    _model = model;
    self.bgImgv.image = Img(model.cover);
    [self.iconBtn sd_setImageWithURL:[NSURL URLWithString:model.getUser.avatar] forState:UIControlStateNormal placeholderImage:Img(@"defult_head")];
    self.nameLabel.text = model.getUser.nickname;
    self.titleLabel.text = model.title;
    
    self.likeBtn.selected = model.islike;
    
    self.addImgV.hidden = [CAUserInfo.shared.followIds containsObject:model.getUser.userId];
    
}


- (void)setInMineView:(BOOL)inMineView{
    _inMineView = inMineView;
    
    if (inMineView) {
        self.likeBtn.hidden = YES;
        [self.rightBtn setImage:Img(@"remove") forState:UIControlStateNormal];
        self.addImgV.hidden = YES;
    }else{
        self.likeBtn.hidden = NO;
        [self.rightBtn setImage:Img(@"cellbutton_home") forState:UIControlStateNormal];
       
    }
}


- (void)initView{
    
    self.bgImgv = [UIImageView new];
    UIView * contentView = self.contentView;
    [contentView addSubview:self.bgImgv];
    [self.bgImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(contentView);
    }];
  
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightBtn setImage:Img(@"cellbutton_home") forState:UIControlStateNormal];
    [contentView addSubview:self.rightBtn];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView).offset(8);
        make.right.equalTo(contentView).offset(-8);
        make.size.mas_equalTo(26);
    }];
    [self.rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];;
    self.iconBtn.layer.cornerRadius = 14;
    self.iconBtn.clipsToBounds = YES;
    [contentView addSubview:self.iconBtn];
    [self.iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(contentView).offset(8);
        make.bottom.equalTo(contentView).offset(-8);
        make.size.mas_equalTo(28);
    }];
    
    self.addImgV = [[UIImageView alloc] initWithImage:Img(@"add_home")];
    [contentView addSubview:self.addImgV];
    [self.addImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self.iconBtn);
        make.size.mas_equalTo(12);
    }];

    
    
    
    [self.iconBtn addTarget:self action:@selector(headClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.nameLabel = [UILabel LabelWithFont:Font(14) color:[UIColor whiteColor] text:@"JACK"];
    [contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.iconBtn);
        make.left.equalTo(self.iconBtn.mas_right).offset(10);
        make.right.equalTo(contentView).offset(-40);
    }];
    
    self.titleLabel = [UILabel LabelWithFont:Font(12) color:[UIColor whiteColor] text:@""];
    [contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconBtn);
        make.bottom.equalTo(self.iconBtn.mas_top).offset(-10);
        make.right.equalTo(contentView).offset(-25);
    }];
    self.titleLabel.numberOfLines = 1;
    
    
    self.likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.likeBtn setImage:Img(@"video_unlike") forState:UIControlStateNormal];
    [self.likeBtn setImage:Img(@"video_like") forState:UIControlStateSelected];
    [contentView addSubview:self.likeBtn];
    [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.iconBtn);
        make.right.equalTo(contentView).offset(-12);
        make.size.mas_equalTo(24);
    }];
    [self.likeBtn addTarget:self action:@selector(likeClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)headClick{
    if (self.inMineView) {
        return;
    }
    
    [Tool Follow:self.model.binduserId];
}


- (void)rightBtnClick{
    
    if (self.inMineView) {
        !self.rightClick?:self.rightClick();
    }else{
        [Tool moreClick:self.model.binduserId];
    }
   
}

- (void)likeClick{
    
    [Tool ShowLoading];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [Tool HideLoading];
       
        
        [self.model SetIslikeInDB:!self.model.islike];
      
        self.likeBtn.selected = self.model.islike;
   
    });
   
}
@end
